package cn.xshi.monitor.web;

import cn.xshi.monitor.service.IotCameraTypeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.monitor.model.IotCameraType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 摄像机厂商类型
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/iotCameraType")
@Api(value = "摄像机厂商类型",tags = "摄像机厂商类型", description = "摄像机厂商类型")
public class IotCameraTypeController extends BaseAction {

    @Resource
    IotCameraTypeService iotCameraTypeService;

    /**
     * 查询并分页
     * @param baseSearch
     */
    @ApiOperation(value="查询并分页", notes="查询并分页")
    @PostMapping(value="/list")
    @Auth(value = "/iotCameraType/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<IotCameraType>> getCameraTypeListByCondition(@RequestBody(required=true) BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        commonHPager(baseSearch);
        List<IotCameraType> iotCameraTypes = iotCameraTypeService.getCameraTypeListByCondition(condition);
        for(IotCameraType iotCameraType:iotCameraTypes){
            if(!StringUtil.isEmpty(iotCameraType.getCreateId())){
                OauthAccountEntity createBy = getAccount(iotCameraType.getCreateId());
                if(null != createBy){
                    iotCameraType.setCreateBy(createBy.getName());
                }
            }
            if(!StringUtil.isEmpty(iotCameraType.getUpdateId())){
                OauthAccountEntity modifiedBy = getAccount(iotCameraType.getUpdateId());
                if(null != modifiedBy){
                    iotCameraType.setModifiedBy(modifiedBy.getName());
                }
            }
        }
        PageInfo<IotCameraType> page = new PageInfo<IotCameraType>(iotCameraTypes);
        return outPageBootStr(page,baseSearch);
    }

    /**
     * 查询单条记录
     * @param id
     */
    @ApiOperation(value="查询单条记录", notes="查询单条记录")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/iotCameraType/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<IotCameraType> getCameraTypeById(@PathVariable("id")String id){
        IotCameraType iotCameraType = iotCameraTypeService.getCameraTypeById(id);
        if(!StringUtil.isEmpty(iotCameraType.getCreateId())){
            OauthAccountEntity createBy = getAccount(iotCameraType.getCreateId());
            if(null != createBy){
                iotCameraType.setCreateBy(createBy.getName());
            }
        }
        if(!StringUtil.isEmpty(iotCameraType.getUpdateId())){
            OauthAccountEntity modifiedBy = getAccount(iotCameraType.getUpdateId());
            if(null != modifiedBy){
                iotCameraType.setModifiedBy(modifiedBy.getName());
            }
        }
        return outDataStr(iotCameraType);
    }

    /**
     * 添加
     * @param iotCameraType
     */
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value="/add")
    public BaseResult addCameraType(@RequestBody IotCameraType iotCameraType){
        int i = 0;
        if(null != iotCameraType){
            i=iotCameraTypeService.addCameraType(iotCameraType);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 修改
     * @param iotCameraType
     */
    @ApiOperation(value="修改", notes="修改")
    @PutMapping(value="/update")
    public BaseResult updateCameraType(@RequestBody IotCameraType iotCameraType){
        int i = 0;
        if(null != iotCameraType){
            i=iotCameraTypeService.updateCameraType(iotCameraType);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 删除
     * @param id
     */
    @ApiOperation(value="删除", notes="删除")
    @DeleteMapping(value="/delete")
    public BaseResult delCameraType(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=iotCameraTypeService.delCameraType(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 查找全部类型
     */
    @ApiOperation(value="查找全部类型", notes="查找全部类型")
    @GetMapping(value="/allList")
    @Auth(value = "/iotCameraType/allList",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<IotCameraType>> getCameraTypeList(){
        Map<String,Object> condition = new HashMap<>();
        List<IotCameraType> iotCameraTypes = iotCameraTypeService.getCameraTypeListByCondition(condition);
        return new BaseResult(iotCameraTypes);
    }
}
